package com.itstyle.mytest.common.aop;

import com.baomidou.mybatisplus.extension.activerecord.Model;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author xianguang
 * @date 2019-12-23 14:36
 */
@Component
@Aspect
public class TestAspect {

    private Lock lock=new ReentrantLock(true);

    /**
     * 指定方法
     */
    @Pointcut("execution(* com.itstyle.mytest.service.IMyTestService.print(..))")
    public void test1(){

    }

    @Before("test1()")
    public  void test() {
        System.out.println("test");
    }



    /**
     * 注解形式
     */
    @Pointcut("@annotation(com.itstyle.mytest.common.aop.TestServiceLock)")
    public void seckillLockAspect(){

    }


    @Around("seckillLockAspect()")
    public  Object around(ProceedingJoinPoint joinPoint) {
        lock.lock();
        Object obj = null;
        try {
            obj = joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
            throw new RuntimeException();
        } finally {
            lock.unlock();
        }
        return obj;
    }
}
